<template>
  <div class="performInfo">
    <el-tabs v-model="tabActive" type="card" @tab-click="tabClick">
      <!-- 专业 -->
      <template v-for="(item,index) in majorsData">
        <el-tab-pane :label="item.majorName" :key="index">
          <div class="tebleDiv">
            <div class="tebleDiv-top">
              <el-collapse
                v-model="item.collapseActive"
                accordion
                @change="(val)=>handleChange(val,item)"
              >
                <addPerformItem
                  :ref="'perform_'+index"
                  :hideBtn="hidePerformBtn"
                  :formData="formData"
                  :orderId="formData.id"
                  :orderStatus="orderStatus"
                  :performData="item.executes"
                  orderType="overhaul"
                  :pageType="pageType"
                  :btnJurisdiction="btnJurisdiction"
                  @changePerformData="val=>havaPerformData=val"
                  @savePerform="savePerformData"
                  @deletePerform="deletePerformData"
                ></addPerformItem>
              </el-collapse>
            </div>
          </div>
        </el-tab-pane>
      </template>
    </el-tabs>
  </div>
</template>

<script>
import {
  getExecutionApi, //获取大修-工单执行列表
  saveExecuteInfo, //保存大修-执行信息
  deleteperformApi //删除执行信息
} from "@/api/orderManagement/overhaul";
import addPerformItem from "@/views/orderManagement/publicModule/addItem";
export default {
  components: {
    addPerformItem
  },
  props: {
    formData: {
      type: Object,
      default: () => {
        return {};
      }
    },
    btnJurisdiction: {
      type: Array,
      default: () => {
        return [];
      }
    },
    pageType: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      orderStatus: "",
      tabActive: "0",
      performData: [],
      majorsData: [],
      currentMajor: {}
    };
  },
  computed: {
    hidePerformBtn() {
      let status = this.formData.status;
      if (status == "IN_EXECUTE" && this.pageType == "edit") {
        return false;
      } else {
        return true;
      }
    }
  },
  watch: {
    "formData.status": {
      handler(val) {
        if (val) {
          this.orderStatus = val.toString();
          this.orderId = this.formData.id;
        }
      },
      deep: true,
      immediate: true
    }
  },
  created() {},
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.getPerformData();
    },
    async getPerformData() {
      let res = await getExecutionApi(this.formData.id);
      let data = res.data || [];
      data.map(i => {
        if (i.executes) {
          i.executes.map(j => {
            let { factStartTime, factEndTime } = j;
            j.factTime = [factStartTime, factEndTime];
            j.orderExecutePart = j.parts;
          });
        }
      });
      this.majorsData = data;
    },
    //tab页的切换
    tabClick(tab, event) {
      this.currentMajor = this.formData[this.tabActive];
    },
    judgeIsComplete() {
      let isComplete = true;
      for (let i = 0; i < this.majorsData.length; i++) {
        let executes = this.majorsData[i].executes || [];
        if (executes.length == 0) {
          isComplete = false;
          this.$message.warning(
            this.majorsData[i].majorName + "专业的执行信息未填写！"
          );
          break;
        }
      }
      return isComplete;
    },
    //保存执行信息
    savePerformData(data, index) {
      data.parts = data.orderExecutePart;
      data.majorId = this.majorsData[this.tabActive].id;
      delete data.orderExecutePart;
      saveExecuteInfo(data).then(res => {
        this.$message.success("操作成功！");
        this.init();
        this.$refs["perform_" + this.tabActive][0].setAttachType(
          index,
          res.data
        );
      });
    },
    deletePerformData(data, index) {
      deleteperformApi(data.id).then(res => {
        this.init();
      });
    }
  }
};
</script>
<style lang="less">
.performInfo {
  .el-tabs__content {
    width: 100%;
  }
}
</style>

